﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_settings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.MyDataBind();
        }
    }

    public void MyDataBind()
    {
        this.cbEnableSmtp.Checked = Params.SmtpEnabled;
        this.conNav.Title = "网站管理";
        if (!Params.SmtpEnabled)
        {
            this.divSmtpSettings.Style.Add("display", "none");
        }
        this.tbSmtpHost.Text = Params.SmtpHost;
        this.tbSmtpPort.Text = Params.SmtpPort.ToString() ;
        this.tbSmtpPwd.Text = Params.SmtpPassword;
        this.tbSmtpUser.Text = Params.SmtpUser;

        this.tbMaxFileSize.Text = Params.MaxFileSize.ToString();

        this.ipTeam.ImportType = LwUser.TeamType;
        this.ipDoctor.ImportType = LwUser.DoctorType;
        this.ipStudent.ImportType = LwUser.StudentType;
        this.ipCategory.ImportType = Category.CategoryStr;

        this.tbMinPaperNum.Text = Params.MinPaperNum.ToString();
        this.tbMaxPaperNum.Text = Params.MaxPaperNum.ToString();

        this.imgX.ImageUrl = Params.ImagesFolderHttp + "x.gif";
        this.btnClearing.Attributes.Add("onclick", "return confirm('确定删除选定数据？')");
    }
    protected void btnSaveSmtp_Click(object sender, EventArgs e)
    {
        string sh = this.tbSmtpHost.Text.Trim();
        string su = this.tbSmtpUser.Text.Trim();
        string sp = this.tbSmtpPwd.Text.Trim();
        string sport = this.tbSmtpPort.Text.Trim();
        if (!Fun.IsNumeric(sport))
        {
            Session.Add(Params.SystemError, "邮箱服务器端口必须为正整数！");
            Response.Redirect(Request.RawUrl);
        }
        int p = Convert.ToInt32(sport);
        string sql = "update Settings set SmtpHost='" + Fun.L(sh) + "',SmtpUser='" + Fun.L(su) + "',SmtpPwd='" + Fun.L(sp) + "',SmtpPort=" +p.ToString()+ " where Guid=" + Fun.GetFirstSettingGuid().ToString();
        Fun.Execute(sql);
        Params.SmtpEnabled = this.cbEnableSmtp.Checked;
        Params.SmtpHost = sh;
        Params.SmtpPassword = sp;
        Params.SmtpPort = p;
        Params.SmtpUser = su;
        Session.Add(Params.SystemError, "保存成功");
        Response.Redirect(Request.RawUrl);
    }
    protected void btnLock_Click(object sender, EventArgs e)
    {
        Params.LockSite = !Params.LockSite;
        this.btnLock.Text = Params.LockSite ? "解锁网站" : "锁定网站";
        string sql = "update Settings set LockSite="+(Params.LockSite?"1":"0");
        Fun.Execute(sql);
        Fun.Refresh("保存成功");
    }
    protected void btnSaveMaxFileSize_Click(object sender, EventArgs e)
    {
        if (!Fun.IsNumeric(this.tbMaxFileSize.Text))
        {
            this.info.SetMessage("必须输入正整数");
            return;
        }
        int mfs = Convert.ToInt32(this.tbMaxFileSize.Text);
        Params.MaxFileSize = mfs;
        string sql = "update Settings set MaxFileSize="+mfs.ToString();
        Fun.Execute(sql);
        Fun.Refresh("保存成功");
    }
    protected void btnSetPaperNum_Click(object sender, EventArgs e)
    {
        if (!Fun.IsNumeric(this.tbMinPaperNum.Text)||!Fun.IsNumeric(this.tbMaxPaperNum.Text))
        {
            this.info.SetMessage("必须输入正整数");
            return;
        }
        int maxPn = Convert.ToInt32(this.tbMaxPaperNum.Text);
        int minPn = Convert.ToInt32(this.tbMinPaperNum.Text);
        Params.MaxPaperNum = maxPn;
        Params.MinPaperNum = minPn;
        string sql = "update Settings set MaxPaperNum=" + maxPn.ToString() + ",MinPaperNum=" + minPn.ToString();
        Fun.Execute(sql);
        Fun.Refresh("保存成功");
    }
    protected void btnDistributeAllStudent_Click(object sender, EventArgs e)
    {
        Fun.DistributeAllStudentThread();
        Fun.Refresh("正在分配......");
    }
    protected void btnClearing_Click(object sender, EventArgs e)
    {
        Fun.ClearWebSite(this.cbTeam.Checked, this.cbDoc.Checked, this.cbStu.Checked, this.cbEmail.Checked, this.cbPap.Checked, this.cbCat.Checked);
        Fun.Refresh("删除成功！");
    }
}
